package redis.clients;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * Created by yunbinan on 16-5-4.
 */
public class RedisServer {
    private JedisCluster jedisCluster = null;
    private Set<HostAndPort> hostAndPortSet = null;
    private JedisPoolConfig config = null;

    private RedisServer() {
        config = new JedisPoolConfig();
        config.setMaxTotal(20);
        config.setMaxIdle(2);
        hostAndPortSet = new HashSet<HostAndPort>();
        hostAndPortSet.add(new HostAndPort("192.168.13.137", 6380));
        //(1)实例化JedisCluster
        jedisCluster = new JedisCluster(hostAndPortSet, 5000, 10, config);
    }

    private static class SingletonFactor {
        private static RedisServer instance = new RedisServer();
    }

    public static RedisServer getInstance() {
        return SingletonFactor.instance;
    }

    public String hSet(String key, Map<String, String> map) {
        return jedisCluster.hmset(key, map);
    }

    public Set<String> zRange(String key, long start, long end) {
        return jedisCluster.zrange(key, start, end);
    }

    public Long zAdd(String key, long score, String value) {
        return jedisCluster.zadd(key, score, value);
    }
    //(7)执行操作 以set为例
    public String set(String key, String value) {
        return jedisCluster.set(key, value);
    }

    public String get(String key) {
        return jedisCluster.get(key);
    }

    public long expire(String key, int second) {
        return jedisCluster.expire(key, second);
    }

    public static void main(String[] args) {
        int count = 0 ;
        while(true){
            RedisServer.getInstance().set("a"+count++, "b");
            try {
                Thread.sleep(30);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}
